// James Fancy, May 2009
// class AlPathTool

function AlPathTool(appUrl) {
    // constructor
    this.$appFolder = "";
    this.$fso = null;

    // public method
    this.initFso = function() {
        this.$fso = new ActiveXObject("Scripting.FileSystemObject");
    }

    this.getPath = function(relFilePath) {
        if (typeof(relFilePath) == "string") {
            var path = this.$appFolder + relFilePath;
            path = path.replace(/[\/\\]+/g, "\\");
            return path;
        } else {
            return this.$appFolder;
        }
    }

    this.getIcons = function() {
        var images = [];
        var iconFolder = this.$fso.GetFolder(this.getPath("icons"));
        if (iconFolder === null) {
            return images;
        }

        for (var files = new Enumerator(iconFolder.Files);
                !files.atEnd(); files.moveNext()) {
            var file = files.item();
            var filename = "" + file.Name;
            if (filename.match(/\.(?:png|jpe?g|gif)$/)) {
                images.push(filename);
            }
        }

        return images;
    }

    this.getWebs = function() {
        var webs = [];
        var websFolder = this.$fso.GetFolder(this.getPath("webs"));
        if (websFolder === null) {
            return images;
        }

        for (var folders = new Enumerator(websFolder.SubFolders);
                !folders.atEnd(); folders.moveNext()) {
            var folder = folders.item();
            var folderName = "" + folder.Name;
            if (folderName.match(/^[^\.]/)) {
                webs.push(folderName);
            }
        }

        return webs;
    }

    // methods
    this.$getAppFolder = function(appUrl) {
        var path = typeof(appUrl) == "string" ? appUrl : new String(window.location);

        if (!path.match(/^file:/i)) {
            return null;
        }

        path = path.replace(/^file:\/+/i, "");
        if (path.match(/\.html?$/i)) {
            path = path.replace(/[^\/]+$/, "");
        }

        if (path.match(/[^\/]$/)) {
            path = path + "/";
        }

        path = path.replace(/\//g, "\\");
        return path;
    };

    // initialize
    this.$appFolder = this.$getAppFolder(appUrl);
}
